package com.seven.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.seven.entity.Problem;
import com.seven.entity.Worker;
import com.seven.service.ProblemService;
import com.seven.service.UsersService;
import com.seven.service.WorkerService;



//员工控制层
public class WorkerServlet extends BaseServlet{
	WorkerService workerService = new WorkerService();
	
	/**
	 * 查询所有
	 * @throws IOException 
	 * @throws ServletException 
	 */
	public void selectAllworker(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		
		PrintWriter out = response.getWriter();
		List<Worker> list = workerService.selectAllWorker();
		JSONArray arr = getArrByList(list);
		out.print(arr);
	}
	
	/**
	 * 增加工人
	 * @throws IOException 
	 */
	public void addWorker(HttpServletRequest request,HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		
		String worker_name = request.getParameter("worker_name");
		String worker_username = request.getParameter("worker_username");
		String worker_password = request.getParameter("worker_password");
		int worker_score = 0;
		int worker_state = 0;
		
		Worker worker = new Worker();
		
		worker.setWorker_name(worker_name);
		worker.setWorker_username(worker_username);
		worker.setWorker_password(worker_password);
		worker.setWorker_score(worker_score);
		worker.setWorker_state(worker_state);
		
		int n  =workerService.addWorker(worker);
		
		out.print(n);
	}
	
	/**
	 * 传值需要:传一个员工id
	 * 删除工人
	 * @throws IOException 
	 */
	public void deleteWorker(HttpServletRequest request,HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		
		String str = request.getParameter("worker_id");
		int worker_id = Integer.parseInt(str);
		System.out.println("1111112222222222223333333333"+worker_id);
		Worker worker = new Worker();
		worker.setWorker_id(worker_id);
		
		int n  = workerService.deleteWorker(worker);
		
		out.print(n);
	}
	
	/**
	 * 传值需要: 传一个问题id, 此问题的员工id
	 * 
	 * 审核通过
	 * 给员工的工分+1
	 * 把问题的处理状态改为已处理
	 * 将员工的状态改为空闲
	 * @throws IOException 
	 */
	public void addScore(HttpServletRequest request,HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		//工分+1&状态改为空闲
		String str = request.getParameter("worker_id");
		int worker_id = Integer.parseInt(str);
		
		Worker worker = new Worker();
		
		worker.setWorker_id(worker_id);
		
		int l  = workerService.addScoreSetstare(worker);
		
		//修改问题的处理状态
		String str1 = request.getParameter("problem_id");
		int problem_id = Integer.parseInt(str1);
		
		Problem problem = new Problem();
		problem.setProblem_id(problem_id);
		
		ProblemService problemService = new ProblemService();
		int m = problemService.updatePromblemToIspass(problem);
		
		out.print(l);
		out.print(m);
	}
	
	public JSONArray getArrByList(List<Worker> list) {
		
		JSONArray arr = new JSONArray();
		JSONObject json;
		for (Worker worker : list) {
			json= new JSONObject();
			json.put("worker_id", worker.getWorker_id());
			json.put("worker_name", worker.getWorker_name());
			json.put("worker_username", worker.getWorker_username());
			json.put("worker_password", worker.getWorker_password());
			json.put("worker_score", worker.getWorker_score());
			
			//员工的状态
			String worker_state = "空闲中";
			int state = worker.getWorker_state();
			if (state==1) {
				worker_state = "忙碌中";
			}else {
				worker_state = "空闲中";
			}
			json.put("worker_state", worker_state);
			
			arr.add(json);
		}
		return arr;
		
	}
}
